package a01_数组;

/**
 * <p>
 * a02_二分查找复习6
 * </p>
 *
 * @author flyduck
 * @since 2025/4/2
 */
public class a02_二分查找复习6 {
    public int search(int[] nums, int target) {
        int leftIdx = 0;
        int rightIdx = nums.length - 1;
        int middleIdx = leftIdx + (rightIdx - leftIdx)/2;

        while (leftIdx <= rightIdx){
            if(target > nums[middleIdx]){
                leftIdx = middleIdx + 1;
                middleIdx = leftIdx + (rightIdx - leftIdx)/2;
            }else if(target < nums[middleIdx]){
                rightIdx = middleIdx - 1;
                middleIdx = leftIdx + (rightIdx - leftIdx)/2;
            }else {
                return middleIdx;
            }
        }
        return -1;

    }
}
